import json

import requests


class WinXinWebHook:
    # 文本消息
    TEXT_TYPE = "text"
    # Markdown消息
    MARKDOWN_TYPE = "markdown"

    def send_text_msg(self, webhook_url, msg):
        """
        发送文本消息
        :param webhook_url: Webhook地址
        :param msg: 消息内容
        """
        message = {
            "msgtype": self.TEXT_TYPE,
            "text": {
                "content": msg
            }
        }
        headers = {
            "Content-Type": "application/json"
        }

        response = requests.post(webhook_url, headers=headers, data=json.dumps(message))
        return response.json()

    def send_markdown_msg(self, webhook_url, markdown_text):
        """
        发送Markdown消息
        :param webhook_url: Webhook地址
        :param markdown_text: Markdown消息文本
        """
        message = {
            "msgtype": self.MARKDOWN_TYPE,
            "markdown": {
                "content": markdown_text
            }
        }
        headers = {
            "Content-Type": "application/json"
        }

        response = requests.post(webhook_url, headers=headers, data=json.dumps(message))
        return response.json()
